package com.lhf.aicloud.config;

import jakarta.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @author : lhf
 * @date : Created in 2024/07/29
 */
@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Resource
    private LoginAuthenticationFilter loginAuthenticationFilter;

    /**
     * 密码加密
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    /**
     * Security 过滤器链
     */
    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        return http
                // 禁用明文验证
                .httpBasic(AbstractHttpConfigurer::disable)
                // 禁用 CSRF 验证
                .csrf(AbstractHttpConfigurer::disable)
                // 禁用默认的登录页面
                .formLogin(AbstractHttpConfigurer::disable)
                // 禁用默认注销页面
                .logout(AbstractHttpConfigurer::disable)
                // 禁用默认 Header (支持 iframe 访问页面)
                .headers(AbstractHttpConfigurer::disable)
                // 禁用 Session(项目中使用的是 JWT 认证)
                .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
                // 配置拦截规则
                .authorizeHttpRequests(authorize -> authorize
                        // 允许访问的资源
                        .requestMatchers(
                                "layui/**",
                                "/js/**",
                                "/css/**",
                                "/login.html",
                                "/index.html",
                                "/register.html",
                                "user/login",
                                "user/register",
                                "captcha/create",
                                "/swagger-ui/**",
                                "/v3/**",
                                "/doc.html",
                                "/webjars/**",
                                "/test/**",
                                "doubao/**",
                                "/"
                        ).permitAll()
                        // 其他请求，需要认证拦截
                        .anyRequest().authenticated()
                )
                // 添加自定义用户登录认证过滤器
                .addFilterBefore(loginAuthenticationFilter, UsernamePasswordAuthenticationFilter.class)
                .build();
    }
}
